package com.changfeng.controller;


import com.changfeng.exception.ChangFengException;
import com.changfeng.exception.ChangFengExceptionEnum;
import com.changfeng.util.FastDFSClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


@RestController
public class UpdateController {

    private  String  FILE_SERVER_URL = "http://mengyanxyz.asuscomm.com:20003/";

    @RequestMapping("/upload")
    public String upload(MultipartFile file) {

        String originalFilename =  file.getOriginalFilename();
        String extName = originalFilename.substring(originalFilename.lastIndexOf(".")+1);//得到扩展名

        try {
            FastDFSClient client = new FastDFSClient("classpath:client.conf");
            String fileId = client.uploadFile(file.getBytes(),extName);
            String url = FILE_SERVER_URL+fileId;
            return url;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ChangFengException(ChangFengExceptionEnum.ERROR);
        }
    }
}
